package com.best.core.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;


/**
 * @ClassName: CommonResult
 * @Package: com.best.core.entity
 * @Description: 统一返回参数实体类
 * @Datetime: 2020/6/5 17:54
 * @Author: nike
 */
@Data
@ApiModel(value = "统一返回参数实体类")
public class CommonResult<T> {

    /**
     * 是否成功
     */
    @ApiModelProperty(value = "是否成功")
    private boolean flag;
    /**
     * 返回码
     */
    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    /**
     * 返回信息  
     */
    @ApiModelProperty(value = "返回信息")
    private String msg;
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;


    public CommonResult(boolean flag, Integer code, String msg, T data) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public CommonResult() {}

    public CommonResult(boolean flag, Integer code, String msg) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
    }

    public static <T> CommonResult<T> buildSuccess() {
        return new CommonResult(true, CommonStatusCode.OK, "success", null);
    }

    public static <T> CommonResult<T> buildSuccess(T data) {
        return new CommonResult(true, CommonStatusCode.OK, "success", data);
    }

    public static <T> CommonResult<T> buildSuccess(boolean flag, Integer code, String message,
            T data) {
        return new CommonResult(flag, code, message, data);
    }

    public static <T> CommonResult<T> buildFail(boolean flag, Integer code, String message) {
        return new CommonResult(flag, code, message);
    }

    public static <T> CommonResult<T> buildFail(String message) {
        return new CommonResult(false, CommonStatusCode.ERROR, message);
    }

}
